package com.upup.cloud.flights;

import com.upup.cloud.flights.entity.FlightSchedule;
import com.upup.cloud.flights.service.FlightService;
import com.upup.cloud.flights.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

import javax.annotation.Resource;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

@Slf4j
@MapperScan({"com.upup.cloud.flights.dao", "com.upup.cloud.flights.dao.base"})
@SpringBootApplication
public class FlightsApplication  implements CommandLineRunner {
    @Autowired
    FlightService flightService;
    @Resource
    RedisService redisService;
    static String chhk_gw ="chhk_hx";
    static String chhk_gj ="chhk_gj";
    public static void main(String[] args) {
        new SpringApplicationBuilder(FlightsApplication.class)
                .web(WebApplicationType.NONE)
                .run(args);
    }
    @Override
    public void run(String... args) throws Exception {
        int i=0;
        List<FlightSchedule> flightList = flightService.getFlightList(); //读取数据库里的航线数据
        System.err.println(flightList.size());
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (FlightSchedule flightSchedule : flightList) {
            String[] days = flightSchedule.getDay().split(","); //把数据库里的day字符串截取为数组
            for (String day : days) {
                if (Integer.parseInt(day)<=120){//限制120天
                    cal.setTime(Date.from(flightSchedule.getUpdatetime().toLocalDate().atStartOfDay(ZoneId.systemDefault()).toInstant())); //获取数据库里的更新时间
                    cal.add(Calendar.DAY_OF_MONTH, Integer.parseInt(day));//加上天数
                    String hx = flightSchedule.getF() + "|" + flightSchedule.getD() + "|" + sdf.format(cal.getTime());//SHA|PVG|2009-09-21
//                    redisService.lpush(chhk_gw,hx);
                    i++;
                }

            }
        }
        System.err.println(i);
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                while (true){
//                    if (redisService.llen(chhk_gw) == 0) {//如果爬虫还没爬取完 就不再获取插队列
//                        List<FlightSchedule> flightList = flightService.getFlightList(); //读取数据库里的航线数据
//                        Calendar cal = Calendar.getInstance();
//                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//                        for (FlightSchedule flightSchedule : flightList) {
//                            String[] days = flightSchedule.getDay().split(","); //把数据库里的day字符串截取为数组
//                            for (String day : days) {
//                                if (Integer.parseInt(day)<=120){//限制120天
//                                    cal.setTime(Date.from(flightSchedule.getUpdatetime().toLocalDate().atStartOfDay(ZoneId.systemDefault()).toInstant())); //获取数据库里的更新时间
//                                    cal.add(Calendar.DAY_OF_MONTH, Integer.parseInt(day));//加上天数
//                                    String hx = flightSchedule.getF() + "|" + flightSchedule.getD() + "|" + sdf.format(cal.getTime());//SHA|PVG|2009-09-21
//                                    redisService.lpush(chhk_gw,hx);
//                                }
//
//                            }
//                        }
//                    }else if (redisService.llen(chhk_gw+"_jq") == 0) {//近期10天的数据队列
//                        List<FlightSchedule> flightList = flightService.getFlightList(); //读取数据库里的航线数据
//                        Calendar cal = Calendar.getInstance();
//                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//                        for (FlightSchedule flightSchedule : flightList) {
//                            String[] days = flightSchedule.getDay().split(","); //把数据库里的day字符串截取为数组
//                            for (String day : days) {
//                                if (Integer.parseInt(day)<=10){//限制120天
//                                    cal.setTime(Date.from(flightSchedule.getUpdatetime().toLocalDate().atStartOfDay(ZoneId.systemDefault()).toInstant())); //获取数据库里的更新时间
//                                    cal.add(Calendar.DAY_OF_MONTH, Integer.parseInt(day));//加上天数
//                                    String hx = flightSchedule.getF() + "|" + flightSchedule.getD() + "|" + sdf.format(cal.getTime());//SHA|PVG|2009-09-21
//                                    redisService.lpush(chhk_gw+"_jq",hx);
//                                }
//
//                            }
//                        }
//                    }else if (redisService.llen(chhk_gj) == 0) {//国际的数据
//                        List<FlightSchedule> flightList = flightService.getFlightGJList(); //读取数据库里的航线数据
//                        Calendar cal = Calendar.getInstance();
//                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//                        for (FlightSchedule flightSchedule : flightList) {
//                            String[] days = flightSchedule.getDay().split(","); //把数据库里的day字符串截取为数组
//                            for (String day : days) {
//                                if (Integer.parseInt(day)<=120){//限制120天
//                                    cal.setTime(Date.from(flightSchedule.getUpdatetime().toLocalDate().atStartOfDay(ZoneId.systemDefault()).toInstant())); //获取数据库里的更新时间
//                                    cal.add(Calendar.DAY_OF_MONTH, Integer.parseInt(day));//加上天数
//                                    String hx = flightSchedule.getF() + "|" + flightSchedule.getD() + "|" + sdf.format(cal.getTime());//SHA|PVG|2009-09-21
//                                    redisService.lpush(chhk_gj,hx);
//                                }
//                            }
//                        }
//                    }else {
//                        try {
//                            //休息十秒
//                            Thread.sleep(1000 * 5);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }
//            }
//        }).start();
    }
}

